<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="utf-8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="utils.DBConnector"%>
<%@page import="utils.Printer"%>



<%	
if (request.getSession(false) == null || request.getSession(false).getAttribute("username") == null){
	response.sendError(HttpServletResponse.SC_FORBIDDEN, "Please login first");
	return;
}
		
%>

<%

boolean isAdmin = request.getSession(false).getAttribute("type").toString().equals("admin");
String username = null;

if (isAdmin){ %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
<link href="style.css" rel="stylesheet" type="text/css" />
<title><%=request.getSession(false).getAttribute("username").toString() %></title>
</head>
<body>
<%@ include file="/WEB-INF/private/body_wrapper_header.html" %>
<%

	username = request.getParameter("username");
}
else{
	username = request.getSession(false).getAttribute("username").toString();	
}
 
%>
<%
boolean isEmpty;
DBConnector dbConn = new DBConnector();

PreparedStatement stmnt = dbConn.getConnection().prepareStatement(
			"SELECT c.course_name, credit_points, group_number " 	+  
			"FROM courses c, user_courses uc " 						+  
			"WHERE uc.username = ? "								+
			"AND c.course_name = uc.course_name AND is_active = TRUE; "	   );

stmnt.setString(1, username);
ResultSet res = stmnt.executeQuery();
isEmpty = !res.next(); //will be true if reultset is empty
res.beforeFirst();
%>


<%
	if (isEmpty){
		if (!isAdmin){
			out.println("<h2>You are currently not registered to any courses</h2>");		
		}
		else{
			out.println("<h2>" + username + " is currently not registered to any courses</h2>");
		}
	
}

else {
	if (!isAdmin){
		out.println("<h2>You are currently registered to the following courses:</h2>");		
	}
	else{
		out.println("<h2>" + username + " is currently registered to the following courses:</h2>");
	}
	
	Printer.printCourses(out, res, "RemoveCourseUser", "user_course_details.jsp", "Remove Course" /* TODO: gal, update hrefUrl parameter*/, false, false, username);
}
dbConn.close();

if (isAdmin){ %>
<%@ include file="/WEB-INF/private/body_wrapper_footer.html" %>
</body>
</html>
 <% } %>